package com.xk857.simpleeffect.fragment.project;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.adapter.ListNoBusinessAdapter;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.Business;
import com.xk857.simpleeffect.utils.OkHttpUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.function.Consumer;

public class ProjectNoFragment extends Fragment {

    private View view;
    private ListView listView;
    private ConstraintLayout emptyLayout;
    private ImageView emptyImg;
    private TextView emptyTxt;
    private Integer projectId;
    private LinearLayout listLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.project_fragment_no, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        if (getArguments() != null) {
            projectId = getArguments().getInt("projectId");
        }
        getBusinessList(true);
    }

    private void getBusinessList(boolean b) {
        Type type = new TypeToken<Result<ArrayList<Business>>>() {}.getType();
        String url = OkHttpUtils.BASE_URL + "/api/v1/business/search/project/" + projectId + "/0";
        OkHttpUtils.getInstance().get(getActivity(), url, type,  b, (Consumer<ArrayList<Business>>) businessList -> {
            if (businessList.size() != 0 && getActivity() != null) {
                getActivity().runOnUiThread(() -> {
                    listView.setAdapter(new ListNoBusinessAdapter(getActivity(), businessList, aBoolean -> {
                        if (getActivity() != null) {
                            getActivity().runOnUiThread(() -> getBusinessList(false));
                        }
                    }));
                    listLayout.setVisibility(View.VISIBLE);
                    emptyLayout.setVisibility(View.GONE);
                });
            }else {
                if (getActivity() != null) {
                    if (getActivity() != null) {
                        getActivity().runOnUiThread(() -> {
                            listLayout.setVisibility(View.GONE);
                            emptyLayout.setVisibility(View.VISIBLE);
                        });
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getBusinessList(false);
    }

    public static ProjectNoFragment newInstance(int projectId) {
        ProjectNoFragment fragment = new ProjectNoFragment();
        Bundle args = new Bundle();
        args.putInt("projectId", projectId);
        fragment.setArguments(args);
        return fragment;
    }

    private void initView(View view) {
        listView = view.findViewById(R.id.list_view);
        emptyLayout = view.findViewById(R.id.empty_layout);
        emptyImg = view.findViewById(R.id.empty_img);
        emptyTxt = view.findViewById(R.id.empty_txt);
        listLayout = view.findViewById(R.id.list_layout);
    }
}
